home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / bibindex / bibindex.c < prev    next >
C/C++ Source or Header  |  1993-05-31  |  33KB  |  1,063 lines

  1. /* ================================================================= *\
  2.  
  3.    bibindex -- a program to index bibtex files, used in conjunction
  4.            with biblook
  5.  
  6.    This program was specifically developed for use with the
  7.    computational geometry bibliographic database, and assumes the
  8.    rules thereof.  The database may be obtained by anonymous ftp
  9.    from cs.usask.ca in the file "pub/geometry/geombib.tar.Z".
  10.  
  11.    Version 1.0 written by Jeff Erickson <jeff@ics.uci.edu>, 27 Mar 92
  12.    Version 2.0 written by Jeff Erickson <jeff@ics.uci.edu>, 17 Jun 92
  13.  
  14.    This program is in the public domain.  You may use it or modify
  15.    it to your heart's content, at your own risk.  Bouquets, brickbats,
  16.    and bug fixes may be sent to Jeff Erickson, jeffe@cs.berkeley.edu.
  17.  
  18.    %Make% gcc -O -o bibindex bibindex.c
  19.  
  20.    Usage: bibindex bibfile [-i field ...]
  21.  
  22.    -----------------------------------------------------------------
  23.  
  24.    HOW IT WORKS:
  25.  
  26.    The bibtex file is read field by field.  The file offset beginning
  27.    each record and each record's citation key are recorded.  A list of
  28.    words is extracted from each field.  These words are placed into
  29.    tables, which remember which records contain them in their
  30.    respective fields.  Once the file has been completely read, the
  31.    hash tables are compacted and sorted.
  32.  
  33.    The hash tables are extensible, since we have to maintain one for
  34.    each possible field type, and static tables would be way too big.
  35.    Initially, each table holds 1K entries, and the tables are doubled
  36.    whenever they reach 75% capacity.  Each table entry is at least 24
  37.    bytes.  If the resulting hash tables use too much memory, the
  38.    entries should be changed to pointers, allocated on the fly.
  39.  
  40.    The entry lists associated with each word are implemented as
  41.    extensible arrays.  Initially, each list holds eight entries.  If a
  42.    new entry is inserted into a full list, the list is doubled first.
  43.  
  44.    The index file has the following format (loosely):
  45.  
  46.     version info
  47.     # entries
  48.     array of offsets into bib file    -- one per entry
  49.     # field types
  50.     array of field names        -- one per field type
  51.     array of            -- one per field type
  52.         # words
  53.         array of            -- one per word
  54.         word            -- in alphabetical order
  55.         # locations
  56.         array of entry #s    -- one per location
  57.  
  58.    There are advantages and disadvantages of having multiple hash
  59.    tables instead of a single table.  I am starting with the premise
  60.    that the lookup program should be very fast.  Consequently, I can't
  61.    make it determine which fields contain a given word.  Doing so
  62.    would require putting ALL of the entry-parsing code into the lookup
  63.    program.  It would also mean potentially parsing a lot of
  64.    extraneous entries to find relatively common words in relatively
  65.    uncommon places (eg, "title edelsbrunner").
  66.  
  67.    If there were a single word table, each word list would have to
  68.    include bitmasks to indicate the appropriate fields along with the
  69.    entry numbers.  Assuming between 16 and 32 field types (the CG bib
  70.    uses about 24), this would triple the size of each entry.  On the
  71.    average, each word occurs in less than two field types.  The
  72.    bitmask approach would also require knowledge of the field names in
  73.    advance; the multiple table approach does not.
  74.  
  75.    -----------------------------------------------------------------
  76.    VERSION HISTORY:
  77.  
  78.    1.0 <jge> 3/26/92    Initial version completed
  79.    1.1 <jge> 3/27/92    Words restricted to letters only; special
  80.             rules added for apostrophes, so that words
  81.             like "didn't" and "O'Rourke" can be handled
  82.             correctly.
  83.    1.2 <jge> 3/30/92    Faster hash function; now compressing hash
  84.             tables before sorting them.  Execution time on
  85.             the CG bib reduced by a factor of thirty-five.
  86.    1.3 <jge> 4/2/92    Toyed with the hash function some more, trying
  87.             to reduce collisions, with limited success.
  88.    1.4 <jge> 4/17/92    Added exit(0) at the end of main()  [I thought
  89.             that was supposed to be automatic!]
  90.  
  91.    2.0 <jge> 6/12/92    First major revision completed.
  92.     1. Major change in file format -- word tables for every
  93.        field instead of just author, title, and keywords.
  94.     2. Word hash tables made extensible.
  95.     3. Fixed bug in GetNextWord that would cause the function
  96.        to return inside nested braces.
  97.     4. Fixed bug in MungeField that would kill everything in an
  98.        entry after an abbreviation.  Abbreviations now go into
  99.        the appropriate hash table with the other words.
  100.     5. Made GetNextWord consider numbers and math expressions
  101.        to be words.
  102.     6. Added WriteWord, resulting in 40% savings in disk space.
  103.     7. Added -i flag and black holes.  Ignoring "oldlabel"
  104.        gives another 15% savings (6/92 version of CGbib).
  105.    2.1 <jge> 7/9/92    Minor bug fixes.
  106.    2.2 Nelson H. F. Beebe <beebe@math.utah.edu> 03-Oct-1992
  107.         Testing with >100K lines of .bib files led to these changes:
  108.     1. Add support for complete BibTeX keyword name syntax with
  109.        iskeychar() function.
  110.     2. Add support for trailing comma after last key = "value" entry.
  111.         3. Record input line number for display in error messages.
  112.         4. Record initial line number of each BibTeX entry for
  113.            display in error messages to better localize error.
  114.     5. Add test for word buffer overflow in MungeField() to prevent
  115.        run-time core dumps, and increase supported word size from
  116.        15 to 31 (a word-length histogram of a 116921-word dictionary
  117.        found words up to 28 characters long, with 1% longer than 15).
  118.        File version increased to 2 because of word size change.
  119.     6. Add typecasts in calls to qsort() and comparisons of
  120.        unsigned short with short, change main() from void to int,
  121.        remove variable from initializer of msg[2], and add void to
  122.        IndexBibFile() definition to allow compilation with C++ as
  123.        well as C for better compile-time checking.
  124.     7. In MungeEntry(), do an ungetc() after key name collection.
  125.        Otherwise, a key="value" pair without a blank before the =
  126.        will not be recognized because the = read on lookahead has
  127.        not been put back.
  128.     8. Revise table compression code in OutputTables() and
  129.        code in FreeTables() to avoid duplicate frees, which is
  130.        a fatal error on many systems, and specified to result
  131.        in undefined behavior by the 1989 ANSI/ISO C Standard.
  132.     9. Define bcopy() as a macro to invoke standard memcpy()
  133.        instead.
  134.        10. Include time.h, unistd.h, and malloc.h to get proper
  135.            function declarations for library routines needed here.
  136.        11. Add DEBUG_MALLOC support.
  137.        12. Change several char* types to const char*.
  138.        13. Correct some typographical errors in comment.
  139.        14. Write UNIX manual pages for bibindex and biblook.
  140.        15. Allow command-line to specify a filename with .bib extension.
  141.        16. Add help support to biblook.
  142.        17. Correct error in FreeTables() in biblook.c; inner loop 
  143.            incremented i instead of j.
  144.    2.3 Bill Jones <jones@cs.usask.ca> 93/01/29
  145.     1. Declarations common to bibindex.c and biblook.c factored out
  146.        to new file biblook.h.
  147.     2. Index type of (signed) short overflows early; created typedef
  148.        Index_t, defined as unsigned short.
  149.     3. Changed hash tables to extend at 75% capacity rather than 50%.
  150.    2.4 Nelson H. F. Beebe <beebe@math.utah.edu> [01-Jun-1993]
  151.         1. Remove some mixed-mode arithmetic.
  152.     2. Increase MAXFIELDS from 64 to 127 to deal with TeX User Group
  153.        bibliography collection
  154.     3. Correct error in GetHashTable(); older versions got into an
  155.        infinite loop if MAXFIELDS field names were already stored, and
  156.        a new one was encountered.
  157.  
  158. \* ================================================================= */
  159.  
  160.  
  161. #include "biblook.h"
  162.  
  163. static long line_number = 1L;        /* for debug messages */
  164. static long initial_line_number = 1L;
  165.  
  166.  
  167. /* ======================= UTILITY FUNCTIONS ======================= */
  168.  
  169. /* ----------------------------------------------------------------- *\
  170. |  void die(const char *msg1, const char *msg2) -- print an error message and die
  171. \* ----------------------------------------------------------------- */
  172. void die(const char *msg1, const char *msg2)
  173. {
  174.     fprintf(stderr,
  175.     "\nError:\tin BibTeX entry starting at line %ld, \
  176. error detected at line %ld:\n\t%s %s\n",
  177.         initial_line_number, line_number, msg1, msg2);
  178.     exit(1);
  179. }
  180.  
  181. /* ----------------------------------------------------------------- *\
  182. |  char safegetc(FILE *fp, const char *where)
  183. |
  184. |  Get the next character safely.  Used by routines that assume that
  185. |  they won't run into the end of file.
  186. \* ----------------------------------------------------------------- */
  187. char safegetc(FILE *fp, const char *where)
  188. {
  189.     char c;
  190.  
  191.     if (feof(fp))
  192.     die("Unexpected end of file in", where);
  193.  
  194.     c = getc(fp);
  195.     if (c == '\n')
  196.     line_number++;
  197.     return (c);
  198. }
  199.  
  200. /* ----------------------------------------------------------------- *\
  201. |  void *safemalloc(unsigned howmuch, const char *msg1, const char *msg2)
  202. |
  203. |  Allocate memory safely.  Used by routines that assume they won't
  204. |  run out of memory.
  205. \* ----------------------------------------------------------------- */
  206. void *safemalloc(unsigned howmuch, const char *msg1, const char *msg2)
  207. {
  208.     register void *tmp = NULL;
  209.  
  210.     tmp = malloc(howmuch);
  211.     if (tmp == NULL)
  212.     die(msg1, msg2);
  213.  
  214.     return tmp;
  215. }
  216.  
  217.  
  218. /* ====================== HASH TABLE FUNCTIONS ===================== *\
  219.  
  220.    The hash tables start small and double whenever they reach 75%
  221.    capacity.  Hashing is performed by going through the string one
  222.    character at a time, multiplying by a constant and adding in the
  223.    new character value each time.  The constant is defined to give
  224.    the same even spread (about size/sqrt(2)) between successive
  225.    multiples, as long as the hash table size is a power of two.
  226.  
  227.    Collisions are resolved by double hashing.  Since the hash table
  228.    size is always a power of two, the secondary hash value has to be
  229.    odd to avoid loops.
  230.  
  231.    The field tables are non-extensible hash tables, otherwise handled
  232.    the same way.  It is probably well worth the effort to fine tune
  233.    the field table hash function in order to avoid collisions.
  234.  
  235.    The field tables associated with ignored fields are black holes.
  236.    Everything is the same, except that InsertEntry doesn't actually
  237.    DO anything.
  238.  
  239. \* ================================================================= */
  240.  
  241. #define MAXFIELDS    127         /* intentionally way too much */
  242.                 /* NB: limited by char type of numfields */
  243.  
  244. #define INIT_HASH_SIZE    256
  245. #define HASH_CONST       1482907        /* prime close to 2^{20.5} */
  246.  
  247. typedef struct        /* Hash table entry */
  248. {
  249.     Word    theword;    /* the hashed word */
  250.     Index_t  number;    /* number of references in the list */
  251.     Index_t  size;    /* real size of reference list */
  252.     Index_t  *refs;    /* actual list of references */
  253. } HashCell, *HashPtr;
  254.  
  255. typedef struct        /* Extensible hash table */
  256. {
  257.     Word    thefield;    /* the field type */
  258.     Index_t  number;    /* number of words in the hash table */
  259.     Index_t  size;    /* real size of the hash table */
  260.     HashPtr words;    /* the actual hash table */
  261. } ExHashTable;
  262.  
  263. static ExHashTable fieldtable[MAXFIELDS];    /* the field tables */
  264. static char        numfields;            /* number of fields */
  265.  
  266.  
  267. /* ----------------------------------------------------------------- *\
  268. |  void InitTables(void)
  269. |
  270. |  Initialize the field tables
  271. \* ----------------------------------------------------------------- */
  272. void InitTables(VOID)
  273. {
  274.     register unsigned short i;
  275.  
  276.     numfields = 0;
  277.     for (i=0; i<MAXFIELDS; i++)
  278.     {
  279.     fieldtable[i].thefield[0] = 0;
  280.     fieldtable[i].number = 0;
  281.     fieldtable[i].size = 0;
  282.     fieldtable[i].words = NULL;
  283.     }
  284. }
  285.  
  286. /* ----------------------------------------------------------------- *\
  287. |  void InitOneField(ExHashTable *htable)
  288. |
  289. |  Initialize one field's hash table
  290. \* ----------------------------------------------------------------- */
  291. void InitOneField(register ExHashTable *htable)
  292. {
  293.     Index_t i;
  294.  
  295.     htable->number = 0;
  296.     htable->size = INIT_HASH_SIZE;
  297.     htable->words = (HashPtr) safemalloc(INIT_HASH_SIZE*sizeof(HashCell),
  298.                      "Can't create hash table for",
  299.                      htable->thefield);
  300.     for (i=0; i<INIT_HASH_SIZE; i++)
  301.     {
  302.     htable->words[i].theword[0] = 0;
  303.     htable->words[i].number = 0;
  304.     htable->words[i].size = 0;
  305.     htable->words[i].refs = NULL;
  306.     }
  307. }
  308.  
  309. /* ----------------------------------------------------------------- *\
  310. |  void FreeTables(void)
  311. |
  312. |  Free the tables
  313. \* ----------------------------------------------------------------- */
  314. void FreeTables(VOID)
  315. {
  316.     register unsigned short i;
  317.     Index_t j;
  318.  
  319.     for (i=0; i < (unsigned short)numfields; i++)
  320.     {
  321.     if (fieldtable[i].words)
  322.     {
  323.         for (j=0; j < fieldtable[i].number; j++)
  324.         {
  325.         if (fieldtable[i].words[j].refs)
  326.             free(fieldtable[i].words[j].refs);
  327.         }
  328.         free(fieldtable[i].words);
  329.     }
  330.     }
  331. }
  332.  
  333.  
  334. /* ----------------------------------------------------------------- *\
  335. |  ExHashTable *GetHashTable(char *field)
  336. |
  337. |  Get the hash table associated with the given field.  If the table
  338. |  is unclaimed, claim it and initialize it.
  339. \* ----------------------------------------------------------------- */
  340. ExHashTable *GetHashTable(char *field)
  341. {
  342.     register unsigned long hash=0;    /* primary hash value    */
  343.     register unsigned long skip=1;    /* secondary hash value */
  344.     register int i;
  345.  
  346.     for (i=0; field[i]; i++)
  347.     {
  348.     hash = (hash*HASH_CONST + field[i]) % MAXFIELDS;
  349.     skip += 2*hash;
  350.     }
  351.  
  352.     while (fieldtable[hash].thefield[0] &&         /* cell not empty and  */
  353.        strcmp(fieldtable[hash].thefield, field)) /* not the right field */
  354.     {
  355.     hash = (hash+skip) % MAXFIELDS;
  356.     }
  357.  
  358.     if (!fieldtable[hash].thefield[0])
  359.     {
  360.     strcpy(fieldtable[hash].thefield, field);
  361.     InitOneField(fieldtable+hash);
  362.     numfields++;
  363.     if (numfields >= MAXFIELDS)    /* NB: NOT > because that produces */
  364.                     /* an infinite while() loop above on */
  365.                     /* next entry! */
  366.         die("too many field names",field);
  367.     }
  368.  
  369.     return fieldtable+hash;
  370. }
  371.  
  372. /* ----------------------------------------------------------------- *\
  373. |  void InitBlackHole(char *field)
  374. |
  375. |  Intitialize a black hole for the given field.
  376. \* ----------------------------------------------------------------- */
  377. void InitBlackHole(char *field)
  378. {
  379.     ExHashTable *hole;
  380.  
  381.     hole = GetHashTable(field);
  382.     free(hole->words);
  383.     hole->words = NULL;
  384. }
  385.  
  386. /* ----------------------------------------------------------------- *\
  387. |  HashPtr GetHashCell(ExHashTable *table, char *word)
  388. |
  389. |  Get the hash table cell associated with the given word.  If the cell
  390. |  is unclaimed, claim it, initialize it, and update the table's word
  391. |  count.
  392. \* ----------------------------------------------------------------- */
  393. HashPtr GetHashCell(ExHashTable *htable, char *word)
  394. {
  395.     register HashPtr table, cell;
  396.     register unsigned long hash=0;    /* primary hash value    */
  397.     register unsigned long skip=1;    /* secondary hash value */
  398.     register int i;
  399.  
  400.     table = htable->words;
  401.  
  402.     for (i=0; word[i]; i++)
  403.     {
  404.     hash = (hash*HASH_CONST + word[i]) % htable->size;
  405.     skip += 2*hash;
  406.     }
  407.  
  408.     while (table[hash].theword[0] &&        /* cell not empty and */
  409.        strcmp(table[hash].theword, word))    /* not the right word */
  410.     {
  411.     hash = (hash+skip) % htable->size;
  412.     }
  413.     cell = table+hash;
  414.  
  415.     if (!cell->theword[0])
  416.     {
  417.     strcpy(cell->theword, word);
  418.     cell->size = 8;
  419.     cell->refs = (Index_t *) safemalloc(cell->size * sizeof(Index_t),
  420.                          "Can't create entry list for",
  421.                          word);
  422.     htable->number++;
  423.     }
  424.     return cell;
  425. }
  426.  
  427. /* ----------------------------------------------------------------- *\
  428. |  void ExtendHashTable(ExHashTable *htable)
  429. |
  430. |  Double the size of the hash table and rehash everything.
  431. \* ----------------------------------------------------------------- */
  432. void ExtendHashTable(ExHashTable *htable)
  433. {
  434.     register HashPtr newcell;
  435.     register HashPtr oldtable;
  436.     Index_t i;
  437.     Index_t oldsize;
  438.  
  439.     oldsize  = htable->size;
  440.     oldtable = htable->words;
  441.  
  442.     htable->number = 0;
  443.     htable->size *= 2;
  444.     htable->words = (HashPtr) safemalloc(sizeof(HashCell)*htable->size,
  445.                      "Can't extend hash table for",
  446.                      htable->thefield);
  447.  
  448.     for (i=0; i < htable->size; i++)
  449.     {
  450.     htable->words[i].theword[0] = 0;
  451.     htable->words[i].number = 0;
  452.     htable->words[i].size = 0;
  453.     htable->words[i].refs = NULL;
  454.     }
  455.  
  456.     for (i=0; i< oldsize; i++)
  457.     {
  458.     if (oldtable[i].theword[0])
  459.     {
  460.         newcell = GetHashCell(htable, oldtable[i].theword);
  461.         *newcell = oldtable[i];
  462.     }
  463.     }
  464.  
  465.     free(oldtable);
  466. }
  467.  
  468. /* ----------------------------------------------------------------- *\
  469. |  void InsertEntry(ExHashTable *htable, char *word, Index_t entry)
  470. |
  471. |  Insert the word/entry pair into the hash table, unless it's
  472. |  already there.
  473. \* ----------------------------------------------------------------- */
  474. void InsertEntry(ExHashTable *htable, char *word, Index_t entry)
  475. {
  476.     register HashPtr cell;
  477.     Index_t *newlist;
  478.  
  479.     if (htable->words == NULL)        /* Is it a black hole? */
  480.     return;
  481.  
  482.     if ((Index_t)(htable->number * 4) > (Index_t)(htable->size * 3))
  483.     ExtendHashTable(htable);
  484.  
  485.     cell = GetHashCell(htable, word);
  486.  
  487.     if (cell->number && (cell->refs[cell->number-1] == entry))
  488.     return;
  489.  
  490.     if (cell->number == cell->size)    /* expand the array */
  491.     {
  492.     cell->size *= 2;
  493.     newlist = (Index_t *) safemalloc(cell->size * sizeof(Index_t),
  494.                        "Can't extend entry list for",
  495.                        word);
  496.  
  497.     bcopy(cell->refs, newlist, cell->number * sizeof(Index_t));
  498.     free(cell->refs);
  499.     cell->refs = newlist;
  500.     }
  501.     cell->refs[cell->number++] = entry;
  502.     if (cell->number <= 0)
  503.     die("hash type overflow", htable->thefield);
  504. }
  505.  
  506.  
  507. /* ================================================================= */
  508.  
  509. /* ----------------------------------------------------------------- *\
  510. |  void WriteWord(FILE *ofp, Word word)
  511. |
  512. |  Output the word in "Pascal" string format -- length byte followed
  513. |  by characters.  This saves some disk space over writing MAXWORD+1 bytes
  514. |  in all cases.
  515. \* ----------------------------------------------------------------- */
  516. void WriteWord(FILE *ofp, Word word)
  517. {
  518.     unsigned char length = strlen(word);
  519.     fwrite((void *) &length, sizeof(char), 1, ofp);
  520.     fwrite((void *) word, sizeof(char), length, ofp);
  521. }
  522.  
  523. /* ----------------------------------------------------------------- *\
  524. |  void OutputTables(FILE *ofp)
  525. |
  526. |  Compress and output the tables, with lots of user feedback.
  527. |  KLUDGE -- Passing strcmp to qsort assumes intimate knowledge of
  528. |  the HashCell and ExhashTable structs.
  529. \* ----------------------------------------------------------------- */
  530. void OutputTables(FILE *ofp)
  531. {
  532.     register HashPtr words;
  533.     register int i, k, count;
  534.     Index_t m, n;
  535.  
  536.     printf("Writing index tables...");
  537.     fflush(stdout);
  538.  
  539.     numfields = 0;        /* recount, ignoring black holes */
  540.     for (i=0; i<MAXFIELDS; i++)
  541.     {
  542.     if (fieldtable[i].words)
  543.     {
  544.         if (i > numfields)
  545.         {
  546.         fieldtable[numfields] = fieldtable[i]; /* copy i-th table */
  547.             fieldtable[i].number = 0; /* then clear i-th table */
  548.             fieldtable[i].size = 0; /* to avoid duplicate free() later */
  549.             fieldtable[i].words = NULL;
  550.         }
  551.         numfields++;
  552.     }
  553.     }
  554.     qsort(fieldtable, (size_t)numfields, sizeof(ExHashTable),
  555.       (int (*)(const void*,const void*))strcmp);
  556.  
  557.     fwrite((void *) &numfields, sizeof(char), 1, ofp);
  558.     for (i=0; i<numfields; i++)
  559.     WriteWord(ofp, fieldtable[i].thefield);
  560.  
  561.     printf("[%d fields]\n", numfields);
  562.  
  563.     for (k=0; k<numfields; k++)
  564.     {
  565.     printf("%2d: %s...", k+1, fieldtable[k].thefield);
  566.     fflush(stdout);
  567.  
  568.     words = fieldtable[k].words;
  569.  
  570.     for (m=0, n=0; m<fieldtable[k].size; m++)
  571.     {
  572.         if (words[m].theword[0])
  573.         {
  574.         if (m > n)
  575.         {
  576.             words[n] = words[m]; /* copy m-th table to n-th */
  577.             words[m].number = 0; /* then clear m-th table */
  578.             words[m].size = 0;    /* to avoid duplicate free() later */
  579.             words[m].refs = (Index_t*)0;
  580.         }
  581.         n++;
  582.         }
  583.     }
  584.     qsort(words, (size_t)fieldtable[k].number, sizeof(HashCell),
  585.           (int (*)(const void*,const void*))strcmp);
  586.  
  587.     fwrite((void *) &(fieldtable[k].number), sizeof(Index_t), 1, ofp);
  588.  
  589.     count = 0;
  590.     for (m=0; m<fieldtable[k].number; m++)
  591.     {
  592.         WriteWord(ofp, words[m].theword);
  593.         fwrite((void *) &(words[m].number), sizeof(Index_t), 1, ofp);
  594.         fwrite((void *) words[m].refs, sizeof(Index_t),
  595.            words[m].number, ofp);
  596.         count += words[m].number;
  597.     }
  598.  
  599.     printf("[%d words, %d references]\n", fieldtable[k].number, count);
  600.     }
  601. }
  602.  
  603.  
  604. /* ============================= INPUT ============================= *\
  605.  
  606.    I'd like this to work with more than just the CG bib, so I can't
  607.    assume very much about the input.  In particular, all white space
  608.    (blanks, tabs, and newlines) is identical most of the time.  On the
  609.    other hand, it would be nice to include any "comments" that
  610.    obviously go with an entry as part of that entry.  Consequently, two
  611.    newlines in a row (possibly separated by other white space) is
  612.    considered a break between entries.  This will give us bogus entries
  613.    for stray "comments", but we can take care that easily enough -- an
  614.    entry is only real if it contains a @ character.
  615.  
  616. \* ================================================================= */
  617.  
  618. /* ----------------------------------------------------------------- *\
  619. |  unsigned long FindNextEntry(FILE *ifp)
  620. |
  621. |  Return the file offset to the next entry in the bib file.  On exit,
  622. |  the file pointer is left at the first comma.  Entries containing no
  623. |  commas, such as @string, are skipped.  The entry begins after the
  624. |  most recent blank line, the end of the last entry, or the beginning
  625. |  of the file.  Returns "-1" if there is no next entry.
  626. \* ----------------------------------------------------------------- */
  627. unsigned long FindNextEntry(FILE *ifp)
  628. {
  629.     char ch;
  630.     char blank = 0;          /* 1 if current line is blank so far */
  631.     char braces = 0;
  632.     char quotes = 0;
  633.     unsigned long offset;
  634.  
  635.     offset = ftell(ifp);
  636.     ch = getc(ifp);
  637.     if (ch == '\n')
  638.     line_number++;
  639.  
  640.     while (1)
  641.     {
  642.     if (feof(ifp))
  643.         return (unsigned long) -1;
  644.  
  645.     if (ch == '@')            /* got a potential entry */
  646.     {
  647.         initial_line_number = line_number; /* record start of entry */
  648.         blank = 0;
  649.         while ((ch != '{') && (ch != '('))
  650.         ch = safegetc(ifp, "FindNextEntry 1");    /* EOF is an */
  651.                             /* error here */
  652.         quotes = 0;
  653.         braces = 0;
  654.         ch = safegetc(ifp, "FindNextEntry 2");
  655.  
  656.         while (quotes || braces ||
  657.            ((ch != ',') && (ch != '}') && (ch != ')')))
  658.         {
  659.         if (ch == '{')
  660.             braces++;
  661.         else if (ch == '}')
  662.             braces--;
  663.         else if ((ch == '"') && !braces)
  664.             quotes = !quotes;
  665.  
  666.         ch = safegetc(ifp, "FindNextEntry 3");
  667.         }
  668.         if (ch == ',')
  669.         {
  670.         ungetc(ch, ifp);
  671.         return offset;
  672.         }
  673.         else
  674.         offset = ftell(ifp);
  675.     }
  676.     else if (ch == '\n')
  677.     {
  678.         if (blank)
  679.         offset = ftell(ifp);
  680.         blank = 1;
  681.     }
  682.     else if (!isspace(ch))
  683.         blank = 0;
  684.     ch = getc(ifp);
  685.     if (ch == '\n')
  686.         line_number++;
  687.     }
  688. }
  689.  
  690. /* ----------------------------------------------------------------- *\
  691. |  int GetNextWord(FILE *ifp, char *word)
  692. |
  693. |  Get the next word in the current field.  A word is any contiguous
  694. |  set of letters and numbers, AFTER the following steps:
  695. |    1. Letters are folded to lower case.  Thus, "Voronoi" is
  696. |       returned as "voronoi"
  697. |    2. All TeX commands, except those in math expressions, are
  698. |       removed, but their arguments are left behind.  Thus,
  699. |       "Erd{\H o}ss" is returned as "erdos".
  700. |    3. All other non-word characters are removed.  Non-word
  701. |       characters inside {{possibly} nested} braces or dollar
  702. |          signs do not delimit words, so they may cause unexpected
  703. |       results.  Thus, "{this example}" is returned as
  704. |       "thisexample".
  705. |    4. TeX commands in math expressions are considered normal
  706. |       text.  Thus, "$O(n\log^2 n)$" is returned as "onlog2n"
  707. |       instead of "onn".  This occasionally gives unexpected or
  708. |       unreadable results.  For example, "$\bigcup_1^n[a_i,b_i]$"
  709. |       is returned as "bigcup1naibi".
  710. |    5. Apostrophes do not delimit words.  Thus, "didn't" is
  711. |       returned as "didnt", and "{\'O}'D{\'u}nlaing" is returned
  712. |       as "odunlaing".
  713. |
  714. |  Returns 1 if a word was actually found, 0 otherwise.
  715. |
  716. |  The file pointer is left at the character immediately following the
  717. |  word.  The input is assumed to be syntactically correct: unbalanced
  718. |  braces, math delimiters, and quotation marks will cause errors.
  719. \* ----------------------------------------------------------------- */
  720. int GetNextWord(FILE *ifp, char *word)
  721. {
  722.     char braces = 0;        /* levels of indented braces */
  723.     char math = 0;        /* 1 if in math expression */
  724.     char inword = 0;        /* 1 if word has started */
  725.     char incmd = 0;        /* 1 if reading TeX command */
  726.     char done = 0;        /* 1 if word is complete */
  727.     char ch = ' ';
  728.     int nchars = 0;        /* how many characters stored in word */
  729.  
  730.     while (!done)
  731.     {
  732.     ch = safegetc(ifp, "GetNextWord 1");
  733.  
  734.     if (isalpha(ch))        /* letters */
  735.     {
  736.         if (!incmd)            /* may be part of TeX command */
  737.         {
  738.         inword = 1;
  739.         if (++nchars <= MAXWORD) /* ignore overflow in word buffer */
  740.             *word++ = tolower(ch);
  741.         }
  742.     }
  743.     else if (isdigit(ch))
  744.     {
  745.         incmd = 0;
  746.         inword = 1;
  747.         if (++nchars <= MAXWORD) /* ignore overflow in word buffer */
  748.             *word++ = ch;
  749.     }
  750.     else if (ch == '\'')        /* ignore apostrophes entirely */
  751.         incmd = 0;
  752.     else if (ch == '\\')         /* beginning of TeX command */
  753.     {
  754.         if (!math)            /* if math, pretend it's text */
  755.         {
  756.         ch = safegetc(ifp, "GetNextWord 2");
  757.         if (isalpha(ch))
  758.             incmd = 1;
  759.         }
  760.     }
  761.     else if (ch == '{')        /* left brace */
  762.     {
  763.         incmd = 0;
  764.         braces++;
  765.     }
  766.     else if (ch == '}')        /* right brace */
  767.     {
  768.         incmd = 0;
  769.         if (!braces)
  770.         {
  771.         ungetc(ch, ifp);
  772.         done = 1;
  773.         }
  774.         else
  775.         braces--;
  776.     }
  777.     else if (ch == '"')        /* double quote */
  778.     {
  779.         incmd = 0;
  780.         if (!braces)
  781.         {
  782.         ungetc(ch, ifp);
  783.         done = 1;
  784.         }
  785.     }
  786.     else if (ch == '$')        /* math */
  787.     {
  788.         incmd = 0;
  789.         math = !math;
  790.         if (math)        /* treat $s as braces */
  791.         braces++;
  792.         else
  793.         braces--;
  794.     }
  795.     else if (incmd)            /* other characters */
  796.         incmd = 0;
  797.     else if (inword && !braces)
  798.         done = 1;
  799.     }
  800.  
  801.     *word = 0;
  802.     return inword;
  803. }
  804.  
  805. const char *badwords[] = BADWORDS;
  806.  
  807. /* ----------------------------------------------------------------- *\
  808. |  void MungeField(FILE *ifp, ExHashTable *htable, Index_t entry)
  809. |
  810. |  Munge the current field.  Dump all words into the given hash table,
  811. |  with the given entry number.  On exit, the file pointer is on the
  812. |  comma or closing character for the entry.
  813. \* ----------------------------------------------------------------- */
  814. void MungeField(FILE *ifp, ExHashTable *htable, Index_t entry)
  815. {
  816.     int i;
  817.     char ch, bad;
  818.     char nextword[MAXWORD+1];
  819.  
  820.     ch = safegetc(ifp, "MungeField");
  821.     while (isspace(ch))
  822.     ch = safegetc(ifp, "MungeField");
  823.  
  824.     if (ch != '=')
  825.     die("= expected in bibliography field", htable->thefield);
  826.  
  827.     ch = safegetc(ifp, "MungeField");
  828.     while (isspace(ch))
  829.     ch = safegetc(ifp, "MungeField");
  830.  
  831.     if (ch != '{' && ch != '"')     /* handle abbreviations */
  832.     {
  833.     for (i=0; !strchr(NONABBREV, ch); i++)
  834.     {
  835.         if (i >= MAXWORD)
  836.         die("word buffer overflow", htable->thefield);
  837.         nextword[i] = tolower(ch);
  838.         ch = safegetc(ifp, "MungeField");
  839.     }
  840.     nextword[i] = 0;
  841.     nextword[MAXWORD] = 0;
  842.     InsertEntry(htable, nextword, entry);
  843.     }
  844.     else
  845.     {
  846.     while (GetNextWord(ifp, nextword))
  847.     {
  848.         if (nextword[1] == 0)    /* ignore single letters */
  849.         continue;
  850.         for (bad=0, i=0; !bad && badwords[i]; i++)
  851.         if (!strcmp(nextword, badwords[i]))
  852.             bad = 1;
  853.         if (!bad)
  854.         {
  855.         nextword[MAXWORD] = 0;        /* truncate to fit */
  856.         InsertEntry(htable, nextword, entry);
  857.         }
  858.     }
  859.     ch = safegetc(ifp, "MungeField");    /* close quote/brace */
  860.     ch = safegetc(ifp, "MungeField");    /* comma (maybe) */
  861.     }
  862.  
  863.     while (isspace(ch))
  864.     ch = safegetc(ifp, "MungeField");
  865.     ungetc(ch, ifp);                 /* comma or close paren/brace */
  866. }
  867.  
  868. /* ----------------------------------------------------------------- *\
  869. |  int iskeychar(char c,int first_char)
  870. |
  871. |  Return 1 if c is a valid keyword character, and 0 otherwise.
  872. |  The rules are different for the first character, so first_char
  873. |  must be set non-zero to select that case.
  874. |
  875. |  The code in bibtex.web in the section on character set dependencies
  876. |  creates a character classification array, id_class[], which permits
  877. |  the following characters (verified by experiment too) in field
  878. |  names:
  879. |
  880. |      A-Z a-z 0-9
  881. |      ! $ & + - . / : ; < > ? @ [ \ ] ^ _ ` | ~ <DELete>
  882. |
  883. \* ----------------------------------------------------------------- */
  884.  
  885. int iskeychar(char c,int first_char)
  886. {
  887.     if (first_char)
  888.         return (isalpha(c) ? 1 : 0);
  889.     else if (isalpha(c) || isdigit(c))
  890.     return (1);
  891.     else if (strchr("!$&+-./:;<>?@[\\]^_`|~\177",(int)c) != (char*)NULL)
  892.     return (1);
  893.     else
  894.     return (0);
  895. }
  896.  
  897. /* ----------------------------------------------------------------- *\
  898. |  void MungeEntry(FILE *ifp, Index_t entry)
  899. |
  900. |  Wander through the entry, extracting words and depositing them in
  901. |  the appropriate hash tables.
  902. \* ----------------------------------------------------------------- */
  903. void MungeEntry(FILE *ifp, Index_t entry)
  904. {
  905.     register char ch;
  906.     Word thefield;
  907.     short i;
  908.  
  909.     ch = safegetc(ifp, "MungeEntry");
  910.     while (ch == ',')
  911.     {
  912.     ch = safegetc(ifp, "MungeEntry");
  913.     while (isspace(ch))
  914.         ch = safegetc(ifp, "MungeEntry");
  915.  
  916.     if ((ch == '}') || (ch == ')'))    /* allow trailing comma after */
  917.         return;            /* last key = "value" entry */
  918.  
  919.     if (!isalpha(ch))
  920.     {
  921.         char msg[2];
  922.         msg[0] = ch;
  923.         msg[1] = 0;
  924.         die("Nonalpha character in field descriptor:", msg);
  925.     }
  926.  
  927.     for (i=0; iskeychar(ch,i==0); i++)
  928.     {
  929.         if (i >= MAXWORD)
  930.         {
  931.         thefield[i] = 0;
  932.         die("field name buffer overflow", thefield);
  933.         }
  934.         thefield[i] = tolower(ch);
  935.         ch = safegetc(ifp, "MungeEntry");
  936.     }
  937.     ungetc(ch, ifp);        /* put back lookahead char */
  938.     thefield[i] = 0;
  939.  
  940.     MungeField(ifp, GetHashTable(thefield), entry);
  941.  
  942.     ch = safegetc(ifp, "MungeEntry");
  943.     }
  944. }
  945.  
  946.  
  947. /* ============================= OUTPUT ============================ */
  948.  
  949. /* ----------------------------------------------------------------- *\
  950. |  IndexBibFile(FILE *ifp, FILE *ofp, char *filename)
  951. |
  952. |  Index a bibtex file.  Input comes from ifp; output goes to ofp.
  953. |  Filename is the name of the bibliography, with no prefix.
  954. \* ----------------------------------------------------------------- */
  955. void
  956. IndexBibFile(FILE *ifp, FILE *ofp, char *filename)
  957. {
  958.     Index_t count=0;
  959.     long curoffset;
  960.     long *offsets;
  961.     long *oldoff;
  962.     Index_t offsize;
  963.     time_t now = time(0);
  964.  
  965.     printf("Indexing %s.bib...", filename);
  966.     fflush(stdout);
  967.  
  968.     offsize = 128;        /* MINIMUM OFFSET LIST SIZE */
  969.     offsets = (long *) malloc(offsize * sizeof(long));
  970.  
  971.     while (!feof(ifp))
  972.     {
  973.     curoffset = FindNextEntry(ifp);
  974.     if (curoffset == (unsigned long) -1)
  975.         break;
  976.  
  977.     offsets[count] = curoffset;
  978.     MungeEntry(ifp, count++);
  979.  
  980.     if (count == offsize)        /* expand full offset array */
  981.     {
  982.         oldoff = offsets;
  983.         offsize *= 2;
  984.         offsets = (long *) malloc(offsize * sizeof(long));
  985.         bcopy(oldoff, offsets, count * sizeof(long));
  986.         free(oldoff);
  987.     }
  988.  
  989.     if (!(count % 500))        /* feedback */
  990.     {
  991.         printf("%d...", count);
  992.         fflush(stdout);
  993.     }
  994.     }
  995.  
  996.     printf("done.\n");
  997.  
  998.     fprintf(ofp, "bibindex %d %d %d %s", FILE_VERSION, MAJOR_VERSION,
  999.         MINOR_VERSION, ctime(&now));
  1000.  
  1001.     printf("Writing file offsets...");
  1002.     fflush(stdout);
  1003.     fwrite((void *) &count, sizeof(Index_t), 1, ofp);
  1004.     fwrite((void *) offsets, sizeof(long), count, ofp);
  1005.     free(offsets);
  1006.     printf("[%d entries]\n", count);
  1007.  
  1008.     OutputTables(ofp);
  1009.  
  1010.     printf("All done!\n");
  1011. }
  1012.  
  1013. /* ----------------------------------------------------------------- *\
  1014. |  The main program
  1015. \* ----------------------------------------------------------------- */
  1016.  
  1017. int main(int argc, char **argv)
  1018. {
  1019.     FILE *ifp;
  1020.     FILE *ofp;
  1021.     char infile[256];
  1022.     char outfile[256];
  1023.     char *p;
  1024.     int i;
  1025.  
  1026. #if DEBUG_MALLOC
  1027.     malloc_debug(2);
  1028. #endif /* DEBUG_MALLOC */
  1029.  
  1030.     if (argc < 2)
  1031.     die("Usage: bibindex bib [-i field...]", "");
  1032.  
  1033.     if (((p = strrchr(argv[1],'.')) != (char*)NULL) &&
  1034.     (strcmp(p, ".bib") == 0))
  1035.     *p = '\0';            /* remove any .bib extension */
  1036.  
  1037.     sprintf(infile, "%s.bib", argv[1]);
  1038.     sprintf(outfile, "%s.bix", argv[1]);
  1039.  
  1040.     ifp = fopen(infile, "r");
  1041.     if (!ifp)
  1042.     die("Can't read", infile);
  1043.  
  1044.     ofp = fopen(outfile, "w");
  1045.     if (!ofp)
  1046.     die("Can't write", outfile);
  1047.  
  1048.     InitTables();
  1049.  
  1050.     if ((argc > 2) && (!strcmp(argv[2], "-i")))
  1051.     for (i=3; i<argc; i++)
  1052.         InitBlackHole(argv[i]);
  1053.  
  1054.     IndexBibFile(ifp, ofp, argv[1]);
  1055.  
  1056.     FreeTables();
  1057.     fclose(ifp);
  1058.     fclose(ofp);
  1059.  
  1060.     exit(0);        /* Argh! */
  1061.     return (0);        /* keep compilers happy */
  1062. }
  1063.